﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using TheDrinksApp.Database;
using TheDrinksApp.Models;

namespace TheDrinksApp.Screen.Order
{
    public partial class GlassSizeScreen : UserControl
    {
        public MainPage parrent;
        private string sizeName;
        private bool isCheckIce = false;
        private bool isCheckLemon = false;
        public bool IsCheckLemon
        {
            get { return isCheckLemon; }
            set { isCheckLemon = value; }
        }

        public bool IsCheckIce
        {
            get { return isCheckIce; }
            set { isCheckIce = value; }
        }

        public GlassSizeScreen(MainPage _parrent)
        {
            InitializeComponent();
            parrent = _parrent;
        }
        private void gridSingle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            sizeName = "SINGLE";
            MixerChangeSize();
        }

        private void gridDouble_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            sizeName = "DOUBLE";
            MixerChangeSize();
        }

        private void gridTriple_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            sizeName = "TRIPLE";
            MixerChangeSize();
        }
        private void MixerChangeSize()
        {
            var imageBrushNonSelect = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri("/Resources/recentOrderItemBackground.png", UriKind.Relative))
            };
            var imageBrushSelected = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri("/Resources/sizeWithControls.png", UriKind.Relative))
            };
            gridDouble.Background = gridSingle.Background = gridTriple.Background = imageBrushNonSelect;
            if (sizeName.Equals("SINGLE"))
            {
                gridSingle.Background = imageBrushSelected;
            }
            else if (sizeName.Equals("DOUBLE"))
            {
                gridDouble.Background = imageBrushSelected;
            }
            else if (sizeName.Equals("TRIPLE"))
            {
                gridTriple.Background = imageBrushSelected;
            }
            Util.currentOrder.size = Util.GetSizeByName(sizeName);
        }


        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            tbName.Text = Util.currentOrder.drink.DrinkName.ToUpper();
            sizeName = Util.currentOrder.size.SizeName.ToUpper();
            MixerChangeSize();
        }

        private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CurrentOrderDAO current = new CurrentOrderDAO();
            current = Util.currentOrder;
            current.quantity = 1;
            current.ice = IsCheckIce;
            current.lemon = IsCheckLemon;
            Util.orderItems.Add(current);
            if (GlobalVariable.version == 1)
            {
                Util.AddToCurrentOrders();
                GlobalVariable.mainPage.SetCurrentNumber();
                GlobalVariable.mainPage.ShowPage(PAGE.CURRENT_ORDER);
            }
            else
            {
                int n = 0;
                int numOrder = 0;
                foreach (CurrentOrderDAO order in Util.orderItems)
                {
                    if (order.quantity > 0)
                    {
                        numOrder++;
                        foreach (CurrentOrderDAO curentOrder in Util.currentOrders)
                        {
                            if (order.drink.DrinkID == curentOrder.drink.DrinkID &&
                                order.mixer.MixerID == curentOrder.mixer.MixerID &&
                                order.ice == curentOrder.ice &&
                                order.lemon == curentOrder.lemon &&
                                order.size.SizeID == curentOrder.size.SizeID)
                            {
                                n++;
                                break;
                            }
                        }
                    }
                }
                
                if ((Util.currentOrders.Count + numOrder - n) > 2)
                {
                    MessageBox.Show("You only can order 2 items in free version");
                    return;
                }
                else
                {
                    Util.AddToCurrentOrders();
                    GlobalVariable.mainPage.SetCurrentNumber();
                    GlobalVariable.mainPage.ShowPage(PAGE.CURRENT_ORDER);
                }
            }
        }

        private void isIce_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            IsCheckIce = !IsCheckIce;
            string imageUrl = (IsCheckIce ? @"/Resources/on.png" : @"/Resources/off.png");
            ImageSourceConverter converter = new ImageSourceConverter();
            isIceImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);
        }

        private void isLemon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            IsCheckLemon = !IsCheckLemon;
            string imageUrl = (IsCheckLemon ? @"/Resources/on.png" : @"/Resources/off.png");
            ImageSourceConverter converter = new ImageSourceConverter();
            isLemonImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);

        }
    }
}
